﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmsSyncServer
{
    public partial class show : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var update = this.Session["Update"];
            if (update != null && DateTime.Now - (DateTime)update < TimeSpan.FromMinutes(5))
            {
                this.Session["Update"] = DateTime.Now;
                if (!this.IsPostBack)
                {
                    this.HandleGet();
                }
            }
            else
            {
                this.Response.Redirect("Passcode.aspx");
            }
        }

        private void HandleGet()
        {
            var pageSize = this.Request.Params["PageSize"];
            var vsLimit = this.ViewState["limit"];

            var limit = 20;
            if (pageSize != null)
            {
                limit = int.Parse(pageSize);
            }
            else if (vsLimit != null)
            {
                limit = (int)vsLimit;
            }

            var pageStart = this.Request.Params["PageStart"];
            var vsSkip = this.ViewState["skip"];

            var skip = 0;
            if (pageStart != null)
            {
                skip = int.Parse(pageStart);
            }
            else if (vsSkip != null)
            {
                skip = (int)vsSkip;
            }

            this.ViewState["limit"] = limit;
            this.ViewState["skip"] = skip;

            this.ShowData();
        }

        private void ShowData()
        {
            using (var db = new SmsSyncEntities())
            {
                var limit = (int)this.ViewState["limit"];
                var skip = (int)this.ViewState["skip"];
                var data = db.Messages.OrderByDescending(m => m.Time).Skip(skip).Take(limit).Select(m => new { m.From, m.To, m.Content, m.Time, m.Device.Code }).ToList();
                this.gv.DataSource = data;
                this.gv.DataBind();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.Current.Text = (((int)this.ViewState["skip"] / (int)this.ViewState["limit"])+1).ToString();
            base.OnPreRender(e);
        }

        protected void First_Click(object sender, EventArgs e)
        {
            this.ViewState["skip"] = 0;
            this.ShowData();
        }

        protected void Prev_Click(object sender, EventArgs e)
        {
            var skip = (int)this.ViewState["skip"];
            var limit = (int)this.ViewState["limit"];
            if (skip <= limit)
            {
                skip = 0;
            }
            else
            {
                skip -= limit;
            }
            this.ViewState["skip"] = skip;
            this.ShowData();
        }

        protected void Next_Click(object sender, EventArgs e)
        {
            var skip = (int)this.ViewState["skip"];
            var limit = (int)this.ViewState["limit"];
            this.ViewState["skip"] = skip + limit;
            this.ShowData();
        }

        protected void Last_Click(object sender, EventArgs e)
        {
            var count = 0;
            using (var db = new SmsSyncEntities())
            {
                count = db.Messages.Count();
            }
            var skip = count / (int)(int)this.ViewState["limit"] * (int)this.ViewState["limit"];
            this.ViewState["skip"] = skip;
            this.ShowData();
        }
    }
}